﻿/*===================================================
* 类名称: DLLHelper
* 类描述:
* 创建人: oream
* 创建时间: 2017/12/24 21:14:48
* 修改人: 
* 修改时间:
* 版本： @version 1.0
=====================================================*/

using System.IO;
using System.Collections;
using UnityEngine;
using RM = Model.ResourcesManager;
using System.Reflection;

namespace Model
{

    /// <summary>
    /// 加载DLL
    /// </summary>
    public static class DllHelper
    {
#if ILR
        public static void LoadHotfixAssembly()
        {
            RM.Instance.LoadBundle("config.assetbundle");
            GameObject config = RM.Instance.GetAsset<GameObject>("config.assetbundle", "config");
            GameObject.Instantiate(config);
            var assBytes = config.GetComponent<LoadConfigComponent>().HotfixDll.bytes;
            var pdbBytes = config.GetComponent<LoadConfigComponent>().HotfixPdb.bytes;
            using (MemoryStream ms = new MemoryStream(assBytes))
            {
                using (MemoryStream mspdb = new MemoryStream(pdbBytes))
                {
                    Init.Instance.AppDomain.LoadAssembly(ms);
                }
            }
        }
#else

        public static Assembly LoadHotfixAssembly()
        {
            RM.Instance.LoadBundle("config.assetbundle");
            GameObject config = RM.Instance.GetAsset<GameObject>("config.assetbundle", "config");
            GameObject.Instantiate(config);
            var assBytes = config.GetComponent<LoadConfigComponent>().HotfixDll.bytes;
            var pdbBytes = config.GetComponent<LoadConfigComponent>().HotfixMdb.bytes;
            RM.Instance.UnloadBundle("config.assetbundle");
            return Assembly.Load(assBytes, pdbBytes);
        }
#endif 
    }
}
